﻿using System;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Messages;
using JustinAngel.Neurons.Framework.Navigation;
using JustinAngel.UnitedNationsNews.Framework;
using Microsoft.Phone.Shell;
using Ninject;

namespace JustinAngel.UnitedNationsNews.Pages.WebBrowser
{
    public class WebBrowserViewModel : VViewModelBase
    {
        public WebBrowserViewModel()
        {
        }

        protected override bool IsAutomaticallyTombstoneAndRestoreViewModel
        {
            get
            {
                return true;
            }
        }

        protected override Type AutomaticallyTombstoneAndRestoreViewModelType
        {
            get
            {
                return typeof(Uri);
            }
        }

        public override void OnDataSet()
        {
            CurrentSource = RealData;
        }

        public string twitterSuccessfulShareUrl = "http://m.twitter.com/share/complete";
        public string twitterLoginFailUri = "res://ieframe.dll/http_default.htm#http://m.twitter.com/share";
        public string facebookSuccessfulShareUrl = "http://m.facebook.com/profile.php?posted_id=";

        public Uri RealData { get { return (Uri) Data; }}

        private Uri _currentSource;
        public Uri CurrentSource
        {
            get { return _currentSource; }
            set
            {
                _currentSource = value;
                RaisePropertyChanged(() => CurrentSource);
                OnCurrentSourceChanged();
            }
        }

        [Inject]
        public IDialogService DialogService { get; set; }

        private void OnCurrentSourceChanged()
        {
            if (CurrentSource == null) return;

            if (CurrentSource.OriginalString.Contains(twitterSuccessfulShareUrl))
            {
                DialogService.ShowMessageBox("Tweet successfully posted.", "Hooray!", MessageBoxButton.OK);
                MessengerInstance.Send(new NavigateBackMessage());
            }
            else if (CurrentSource.OriginalString.Contains(facebookSuccessfulShareUrl))
            {
                DialogService.ShowMessageBox("Facebook link successfully posted.", "Hooray!", MessageBoxButton.OK);
                MessengerInstance.Send(new NavigateBackMessage());
            }
            else if (CurrentSource.OriginalString == twitterLoginFailUri)
            {
                DialogService.ShowMessageBox("Wrong twitter user name or password.", "Try again!", MessageBoxButton.OK);
                MessengerInstance.Send(new NavigateBackMessage());
            }
        }

        private bool _isLoadingPage;
        public bool IsLoadingPage
        {
            get { return _isLoadingPage; }
            set
            {
                _isLoadingPage = value;
                RaisePropertyChangedAndUpdateVSM(() => IsLoadingPage, value);
            }
        }

        public ICommand LoadingPage
        {
            get { return new RelayCommand(() => IsLoadingPage = true); }
        }


        public ICommand LoadedPage
        {
            get { return new RelayCommand(() => IsLoadingPage = false); }
        }
    }
}